﻿/*
Copyright (c) 2008 William Duff

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace LiveUpload.Facebook
{
    public class AccountSession
    {
        #region Constructors

        /// <summary>
        /// Constructs a Session object with a key and secret specific to a Facebook user's session.
        /// </summary>
        public AccountSession(string key, string secret, string ownerId, 
            double expiresUnixTimestamp)
        {
            this.key = key;
            this.secret = secret;
            this.ownerId = ownerId;
            this.expires = FacebookParser.ConvertFromUnixTimestamp(expiresUnixTimestamp);
            this.expiresUnixTimestamp = expiresUnixTimestamp;
        }

        #endregion

        #region Private Fields

        private string key;
        private string secret;
        private string ownerId;
        private DateTime? expires;
        private double expiresUnixTimestamp;

        #endregion

        #region Public Fields

        /// <summary>
        /// The Facebook session key specific to a Facebook user's session.
        /// </summary>
        public string Key
        {
            get { return this.key; }
        }

        /// <summary>
        /// The Facebook session secret specific to a Facebook user's session.
        /// </summary>
        public string Secret
        {
            get { return this.secret; }
        }
        
        /// <summary>
        /// The id of the user to which this session belongs.
        /// </summary>
        public string OwnerId
        {
            get { return this.ownerId; }
        }

        /// <summary>
        /// The time at which this session expires. Null for never.
        /// </summary>
        public DateTime? Expires
        {
            get { return this.expires; }
        }

        /// <summary>
        /// The time at which this session expires in Unix time. Zero for never.
        /// </summary>
        public double ExpiresUnixTimestamp
        {
            get { return this.expiresUnixTimestamp; }
        }

        #endregion

        #region Public Methods

        public bool IsExpired()
        {
            if (this.expiresUnixTimestamp > 0d)
            {
                return FacebookParser.ConvertToUnixTimestamp(DateTime.UtcNow) > 
                    this.expiresUnixTimestamp;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}